package Class.TextWindow;

import org.w3c.dom.Text;

import Class.Handler.*;
import Class.Dialog.*;
import Class.TextWindow.*;
import Class.Main.*;

import javax.swing.*;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

public class TextWindow extends JFrame{
    //添加应用图标
    ImageIcon mainIcon = new ImageIcon("src/image/Icon.ico");

    //创建菜单条
    private JMenuBar menuBar = new JMenuBar();

    //字体默认值
    private int defaultFontSize = 20;

    //设置菜单监听器
    private MenuHandler menuHandler = new MenuHandler(this);

    //设置撤销监听器
    private UndoManager undoManager = new UndoManager();

    //autoAround要在外部类被使用，定义为成员变量而不是方法变量
    private JCheckBoxMenuItem autoAround = new JCheckBoxMenuItem("自动换行(W)");

    private JCheckBoxMenuItem statusBar = new JCheckBoxMenuItem("状态栏(S)");

    //创建弹出式菜单
    private JPopupMenu popupMenu = new JPopupMenu();

    private JPanel statusPanel = new JPanel();
    private JLabel statusLabel= new JLabel("|第1行，第1列|",SwingConstants.LEFT);//设置光标监听，读取行列数据
    private JLabel virtualStatusLabel = new JLabel("                                                                                                         ");

    //创建文本区域组件
    JTextArea textArea = new JTextArea(6,40);

    //设置判断文本是否保存的参数
    private boolean hasSaved = false;

    //文件保存的绝对路径
    private String filePath = null;

    public TextWindow() {
        //初始化命名
        super("未命名文档.txt");

        createFileMenu();
        createEditMenu();
        createFormatMenu();
        createCheckMenu();
        createHelpMenu();
        createPopupMenu();

        //把菜单条放入到TextWindow中
        this.setJMenuBar(menuBar);

        //滚动面板
        JScrollPane textScrollPane = new JScrollPane(textArea);
        //textArea默认布局方式
        this.getContentPane().add(textScrollPane);
        statusPanel.add(statusLabel);//西边对齐
        statusPanel.add(virtualStatusLabel,BorderLayout.EAST);
        this.getContentPane().add(statusPanel,BorderLayout.SOUTH);

        //窗体图标、大小、位置、开关设置
        this.setIconImage(mainIcon.getImage());   //设置图标
        this.setBackground(Color.white);
        setBounds(0, 0, 807, 537);
        setLocationRelativeTo(null);//居中显示
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(true);
        setVisible(true);


        //修改字体，将字体放大
        Font defaultFont = textArea.getFont();
        Font font = new Font(defaultFont.getFamily(),defaultFont.getStyle(),defaultFontSize);
        textArea.setFont(font);
        //设置textArea
        textArea.setTabSize(2);//设置缩进为2个字符
        //绑定textArea和UndoManger
        textArea.getDocument().addUndoableEditListener(undoManager);

        //添加内容区监听，随时修改hasSaved值
        TextAreaHandler textAreaHandler = new TextAreaHandler(this);
        textArea.getDocument().addDocumentListener(textAreaHandler);
        //设置光标监听
        textArea.addCaretListener(textAreaHandler);

        textArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e){
                if(e.getButton() == e.BUTTON3){
                    System.out.println("1");
                    popupMenu.show(e.getComponent(),e.getX(),e.getY());
                }
            }
        });


    }
//---------------------------------------------------------------------------

    private void createPopupMenu(){

        JMenuItem backout = new JMenuItem("撤销(U)");
        backout.setMnemonic(KeyEvent.VK_U);
        backout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,KeyEvent.CTRL_DOWN_MASK));
        backout.setActionCommand("backout");
        backout.addActionListener(menuHandler);
        backout.setIconTextGap(5);

        JMenuItem cut =new JMenuItem("剪切(T)");
        cut.setMnemonic(KeyEvent.VK_T);
        cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.CTRL_DOWN_MASK));
        cut.setActionCommand("cut");
        cut.addActionListener(menuHandler);
        cut.setIconTextGap(5);

        JMenuItem copy =new JMenuItem("复制(C))");
        copy.setMnemonic(KeyEvent.VK_C);
        copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,KeyEvent.CTRL_DOWN_MASK));
        copy.setActionCommand("copy");
        copy.addActionListener(menuHandler);
        copy.setIconTextGap(5);

        JMenuItem stick =new JMenuItem("粘贴(P)");
        stick.setMnemonic(KeyEvent.VK_P);
        stick.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,KeyEvent.CTRL_DOWN_MASK));
        stick.setActionCommand("stick");
        stick.addActionListener(menuHandler);
        stick.setIconTextGap(5);

        JMenuItem deletePopup =new JMenuItem("删除(L)");
        deletePopup.setMnemonic(KeyEvent.VK_L);
        deletePopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
        deletePopup.setActionCommand("deletePopup");
        deletePopup.addActionListener(menuHandler);
        deletePopup.setIconTextGap(5);

        JMenuItem checkAll =new JMenuItem("全选(A)");
        checkAll.setMnemonic(KeyEvent.VK_A);
        checkAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,KeyEvent.CTRL_DOWN_MASK));
        checkAll.setActionCommand("checkAll");
        checkAll.addActionListener(menuHandler);
        checkAll.setIconTextGap(5);

        popupMenu.add(backout);
        popupMenu.addSeparator();
        popupMenu.add(copy);
        popupMenu.add(stick);
        popupMenu.add(deletePopup);
        popupMenu.addSeparator();
        popupMenu.add(checkAll);

    }

    private void createFileMenu(){

        JMenu fileMenu = new JMenu("文件(F)");
        fileMenu.setMnemonic(KeyEvent.VK_F);//设置助击符


        //创建文件菜单项组件及组件的快捷键
        JMenuItem newBuild = new JMenuItem("新建(N)");
        newBuild.setMnemonic(KeyEvent.VK_N);
        newBuild.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_DOWN_MASK));
        newBuild.setActionCommand("newBuild");
        newBuild.addActionListener(menuHandler);
        newBuild.setIconTextGap(5);


        JMenuItem open = new JMenuItem("打开(O)");
        open.setMnemonic(KeyEvent.VK_O);
        open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.CTRL_DOWN_MASK));
        open.setActionCommand("open");
        open.addActionListener(menuHandler);
        open.setIconTextGap(5);

        
        JMenuItem save = new JMenuItem("保存(S)");
        save.setMnemonic(KeyEvent.VK_S);
        save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_DOWN_MASK));
        save.setActionCommand("save");
        save.addActionListener(menuHandler);
        save.setIconTextGap(5);

        JMenuItem saveAs = new JMenuItem("另存为(A)");
        saveAs.setMnemonic(KeyEvent.VK_A);
        saveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_DOWN_MASK+KeyEvent.SHIFT_DOWN_MASK));
        saveAs.setActionCommand("saveAs");
        saveAs.addActionListener(menuHandler);
        saveAs.setIconTextGap(5);
        
        
        JMenuItem exit = new JMenuItem("退出(X)");
        exit.setMnemonic(KeyEvent.VK_X);
//      退出无快捷键
        exit.setActionCommand("exit");
        exit.addActionListener(menuHandler);
        exit.setIconTextGap(5);

        fileMenu.add(newBuild);
        fileMenu.add(open);
        fileMenu.add(save);
        fileMenu.add(saveAs);
        fileMenu.addSeparator();
        fileMenu.add(exit);

        menuBar.add(fileMenu);

    }


    private void createEditMenu(){
        JMenu editMenu = new JMenu("编辑(E)");
        editMenu.setMnemonic(KeyEvent.VK_E);

        //创建编辑菜单项各组件
        JMenuItem backout = new JMenuItem("撤销(U)");
        backout.setMnemonic(KeyEvent.VK_U);
        backout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,KeyEvent.CTRL_DOWN_MASK));
        backout.setActionCommand("backout");
        backout.addActionListener(menuHandler);
        backout.setIconTextGap(5);

        JMenuItem cut =new JMenuItem("剪切(T)");
        cut.setMnemonic(KeyEvent.VK_T);
        cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.CTRL_DOWN_MASK));
        cut.setActionCommand("cut");
        cut.addActionListener(menuHandler);
        cut.setIconTextGap(5);

        JMenuItem copy =new JMenuItem("复制(C))");
        copy.setMnemonic(KeyEvent.VK_C);
        copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,KeyEvent.CTRL_DOWN_MASK));
        copy.setActionCommand("copy");
        copy.addActionListener(menuHandler);
        copy.setIconTextGap(5);

        JMenuItem stick =new JMenuItem("粘贴(P)");
        stick.setMnemonic(KeyEvent.VK_P);
        stick.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,KeyEvent.CTRL_DOWN_MASK));
        stick.setActionCommand("stick");
        stick.addActionListener(menuHandler);
        stick.setIconTextGap(5);

        JMenuItem delete =new JMenuItem("删除(L)");
        delete.setMnemonic(KeyEvent.VK_L);
        delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
        delete.setActionCommand("delete");
        delete.addActionListener(menuHandler);
        delete.setIconTextGap(5);

        JMenuItem find =new JMenuItem("查找(F)");
        find.setMnemonic(KeyEvent.VK_F);
        find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,KeyEvent.CTRL_DOWN_MASK));
        find.setActionCommand("find");
        find.addActionListener(menuHandler);
        find.setIconTextGap(5);

        JMenuItem alter =new JMenuItem("替换(R)");
        alter.setMnemonic(KeyEvent.VK_R);
        alter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,KeyEvent.CTRL_DOWN_MASK));
        alter.setActionCommand("alter");
        alter.addActionListener(menuHandler);
        alter.setIconTextGap(5);

        JMenuItem turnTo =new JMenuItem("转到(G)");
        turnTo.setMnemonic(KeyEvent.VK_G);
        turnTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,KeyEvent.CTRL_DOWN_MASK));
        turnTo.setActionCommand("turnTo");
        turnTo.addActionListener(menuHandler);
        turnTo.setIconTextGap(5);


        JMenuItem checkAll =new JMenuItem("全选(A)");
        checkAll.setMnemonic(KeyEvent.VK_A);
        checkAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,KeyEvent.CTRL_DOWN_MASK));
        checkAll.setActionCommand("checkAll");
        checkAll.addActionListener(menuHandler);
        checkAll.setIconTextGap(5);
        
        JMenuItem timeAndDate =new JMenuItem("时间/日期(D)");
        timeAndDate.setMnemonic(KeyEvent.VK_D);
        timeAndDate.setAccelerator(KeyStroke.getKeyStroke("F5"));
        timeAndDate.setActionCommand("timeAndDate");
        timeAndDate.addActionListener(menuHandler);
        timeAndDate.setIconTextGap(5);

        //组装编辑菜单
        editMenu.add(backout);
        editMenu.addSeparator();
        editMenu.add(cut);
        editMenu.add(copy);
        editMenu.add(stick);
        editMenu.add(delete);
        editMenu.addSeparator();
        editMenu.add(find);
        editMenu.add(alter);
        editMenu.add(turnTo);
        editMenu.addSeparator();
        editMenu.add(checkAll);
        editMenu.add(timeAndDate);

        menuBar.add(editMenu);

    }

    private void createFormatMenu(){
        JMenu formatMenu = new JMenu("格式(O)");
        formatMenu.setMnemonic(KeyEvent.VK_O);

        //创建格式菜单项组件
        autoAround.setSelected(false);//设置启动默认自动换行
        autoAround.setMnemonic(KeyEvent.VK_W);
        autoAround.setActionCommand("autoAround");
        autoAround.addActionListener(menuHandler);
        autoAround.setIconTextGap(5);

        JMenuItem typeFace = new JMenuItem("字体(F)");
        typeFace.setMnemonic(KeyEvent.VK_F);
        typeFace.setActionCommand("typeFace");
        typeFace.addActionListener(menuHandler);
        typeFace.setIconTextGap(5);
        
        //组装格式菜单
        formatMenu.add(autoAround);
        formatMenu.add(typeFace);

        menuBar.add(formatMenu);

    }

    private void createCheckMenu(){
        JMenu checkMenu = new JMenu("查看(V)");
        checkMenu.setMnemonic(KeyEvent.VK_V);

        //创建查看菜单项组件
        JMenu scale = new JMenu("缩放(Z)");
        scale.setMnemonic(KeyEvent.VK_Z);
        scale.setIconTextGap(5);

        JMenuItem enlarge = new JMenuItem("放大(I)");
        enlarge.setMnemonic(KeyEvent.VK_I);
        enlarge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS,KeyEvent.CTRL_DOWN_MASK));
        enlarge.setActionCommand("enlarge");
        enlarge.addActionListener(menuHandler);
        enlarge.setIconTextGap(5);


        JMenuItem shrink = new JMenuItem("缩小(O)");
        shrink.setMnemonic(KeyEvent.VK_O);
        shrink.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,KeyEvent.CTRL_DOWN_MASK));
        shrink.setActionCommand("shrink");
        shrink.addActionListener(menuHandler);
        shrink.setIconTextGap(5);

        JMenuItem defaultScaling = new JMenuItem("恢复默认缩放");
        defaultScaling.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0,KeyEvent.CTRL_DOWN_MASK));
        defaultScaling.setActionCommand("defaultScaling");
        defaultScaling.addActionListener(menuHandler);
        defaultScaling.setIconTextGap(5);

        statusBar.setSelected(true);//设置默认显示状态栏
        statusBar.setMnemonic(KeyEvent.VK_S);
        statusBar.setActionCommand("statusBar");
        statusBar.addActionListener(menuHandler);
        statusBar.setIconTextGap(5);
        
        //组装查看菜单
        checkMenu.add(scale);
        scale.add(enlarge);
        scale.add(shrink);
        scale.add(defaultScaling);
        checkMenu.addSeparator();
        checkMenu.add(statusBar);

        menuBar.add(checkMenu);

    }

    private void createHelpMenu(){
        JMenu helpMenu = new JMenu("帮助(H)");
        helpMenu.setMnemonic(KeyEvent.VK_H);

        JMenuItem checkHelp = new JMenuItem("查看帮助");
        checkHelp.setActionCommand("checkHelp");
        checkHelp.addActionListener(menuHandler);
        checkHelp.setIconTextGap(5);

        JMenuItem aboutTextbook = new JMenuItem("关于记事本");
        aboutTextbook.setActionCommand("aboutTextbook");
        aboutTextbook.addActionListener(menuHandler);
        aboutTextbook.setIconTextGap(5);

        //组装帮助菜单
        helpMenu.add(checkHelp);
        helpMenu.add(aboutTextbook);

        menuBar.add(helpMenu);

    }


    public boolean isHasSaved() {
        return hasSaved;
    }

    public void setHasSaved(boolean hasSaved) {
        this.hasSaved = hasSaved;
    }

    public JTextArea getTextArea() {
        return textArea;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public UndoManager getUndoManger(){
        return undoManager;
    }

    public JCheckBoxMenuItem getAutoAround(){
        return  autoAround;
    }

    public int getDefaultFontSize(){
        return  defaultFontSize;
    }

    public JPanel getStatusPanel(){
        return statusPanel;
    }

    public JCheckBoxMenuItem getStatusBar(){
        return  statusBar;
    }

    public JLabel getStatusLabel(){
        return statusLabel;
    }

}